package com.lineage.server.model;

import com.lineage.server.datatables.InnKeyTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_NPCPack_Item;
import com.lineage.server.serverpackets.S_RemoveObject;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.timecontroller.server.ServerDeleteItemTimer;
import com.lineage.server.world.World;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1GroundInventory extends L1Inventory {
  private static final long serialVersionUID = 1L;
  
  private static final Log _log = LogFactory.getLog(L1GroundInventory.class);
  
  private void setTimer(L1ItemInstance item) {
    try {
      if (item.getItemId() == 40515)
        return; 
      if (L1HouseLocation.isInHouse(getX(), getY(), getMapId()))
        return; 
      if (!ServerDeleteItemTimer.contains(item))
        ServerDeleteItemTimer.add(item); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public L1GroundInventory(int objectId, int x, int y, short map) {
    try {
      setId(objectId);
      setX(x);
      setY(y);
      setMap(map);
      World.get().addVisibleObject(this);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void onPerceive(L1PcInstance perceivedFrom) {
    try {
      Iterator<L1ItemInstance> iterator = getItems().iterator();
      while (iterator.hasNext()) {
        L1ItemInstance item = iterator.next();
        if (perceivedFrom.get_showId() == item.get_showId() && !perceivedFrom.knownsObject((L1Object)item)) {
          perceivedFrom.addKnownObject((L1Object)item);
          perceivedFrom.sendPackets((ServerBasePacket)new S_NPCPack_Item(item));
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void insertItem(L1ItemInstance item) {
    if (item.getCount() <= 0L)
      return; 
    try {
      setTimer(item);
      Iterator<L1PcInstance> iterator = World.get().getRecognizePlayer((L1Object)item).iterator();
      while (iterator.hasNext()) {
        L1PcInstance pc = iterator.next();
        if (pc.get_showId() == item.get_showId()) {
          pc.sendPackets((ServerBasePacket)new S_NPCPack_Item(item));
          pc.addKnownObject((L1Object)item);
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void updateItem(L1ItemInstance item) {
    try {
      Iterator<L1PcInstance> iterator = World.get().getRecognizePlayer((L1Object)item).iterator();
      while (iterator.hasNext()) {
        L1PcInstance pc = iterator.next();
        if (pc.get_showId() == item.get_showId())
          pc.sendPackets((ServerBasePacket)new S_NPCPack_Item(item)); 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void onRemoveItem(L1ItemInstance item) {
    try {
      Iterator<L1PcInstance> iterator = World.get().getRecognizePlayer((L1Object)item).iterator();
      while (iterator.hasNext()) {
        L1PcInstance pc = iterator.next();
        pc.sendPackets((ServerBasePacket)new S_RemoveObject((L1Object)item));
        pc.removeKnownObject((L1Object)item);
      } 
      this._items.remove(item);
      if (this._items.size() == 0)
        World.get().removeVisibleObject(this); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void deleteItem(L1ItemInstance item) {
    try {
      Iterator<L1PcInstance> iterator = World.get().getRecognizePlayer((L1Object)item).iterator();
      while (iterator.hasNext()) {
        L1PcInstance pc = iterator.next();
        pc.sendPackets((ServerBasePacket)new S_RemoveObject((L1Object)item));
        pc.removeKnownObject((L1Object)item);
      } 
      int itemid = item.getItemId();
      if (itemid == 40312)
        InnKeyTable.DeleteKey(item); 
      if (itemid == 82504)
        InnKeyTable.DeleteKey(item); 
      this._items.remove(item);
      if (this._items.size() == 0)
        World.get().removeVisibleObject(this); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
}
